package com.web.controller;

import com.alibaba.fastjson2.JSON;
import com.web.entity.ResponseResult;
import com.web.entity.Student;
import com.web.service.StudentsService;
import com.web.service.impl.StudentsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lenovo
 * @Description
 * @Date2025-3-1 14:22
 */
@WebServlet("/searchStudent")
public class StudentSearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //设置相应类型
        resp.setContentType("application/json;charset=UTF-8");
        String keyword = req.getParameter("keyword");
        StudentsService studentsService = new StudentsServiceImpl();
        List<Student> students = studentsService.searchStudentsByName(keyword);
        //返回结果
        String str1 = JSON.toJSONString(new ResponseResult(200, "ok", students));
        resp.getWriter().write(str1);
    }

}

